package com.iteaj.iboot.plugin.crawler.controller;

import java.io.IOException;
import java.util.List;
import com.iteaj.framework.logger.Logger;
import com.iteaj.framework.result.Result;
import com.iteaj.framework.security.Logical;
import com.iteaj.iboot.plugin.crawler.CrawlerProperties;
import org.springframework.web.bind.annotation.*;
import com.iteaj.framework.security.CheckPermission;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iteaj.iboot.plugin.crawler.entity.SiteContent;
import com.iteaj.iboot.plugin.crawler.service.ISiteContentService;
import com.iteaj.framework.BaseController;

/**
 * 采集的站点内容管理
 * @author iteaj
 * @since 2024-10-05
 */
@RestController
@RequestMapping("/crawler/siteContent")
public class SiteContentController extends BaseController {

    private final CrawlerProperties properties;
    private final ISiteContentService siteContentService;

    public SiteContentController(CrawlerProperties properties, ISiteContentService siteContentService) {
        this.properties = properties;
        this.siteContentService = siteContentService;
    }

    /**
    * 列表查询
    * @param page 分页
    * @param entity 搜索条件
    */
    @GetMapping("/view")
    @Logger("浏览采集的站点内容功能")
    @CheckPermission({"crawler:siteContent:view"})
    public Result<IPage<SiteContent>> list(Page<SiteContent> page, SiteContent entity) {
        return this.siteContentService.page(page, entity);
    }

    /**
    * 获取编辑记录
    * @param id 记录id
    */
    @GetMapping("/edit")
    @CheckPermission({"crawler:siteContent:edit"})
    public Result<SiteContent> getById(Long id) {
        return this.siteContentService.detailById(id);
    }

    /**
    * 新增或者更新记录
    * @param entity
    */
    @PostMapping("/saveOrUpdate")
    @Logger("新增或者更新采集的站点内容记录")
    @CheckPermission(value = {"crawler:siteContent:edit", "crawler:siteContent:add"}, logical = Logical.OR)
    public Result<Boolean> saveOrUpdate(@RequestBody SiteContent entity) {
        return this.siteContentService.saveOrUpdate(entity);
    }

    /**
    * 删除指定记录
    * @param idList
    */
    @PostMapping("/del")
    @Logger("删除采集的站点内容记录")
    @CheckPermission({"crawler:siteContent:del"})
    public Result<Boolean> remove(@RequestBody List<Long> idList) {
        return this.siteContentService.removeByIds(idList);
    }

    /**
     * 视频剪辑
     * @param entity
     * @return
     */
    @PostMapping("/cut")
    public Result<Boolean> cut(SiteContent entity) {
        return success();
    }

    /**
     * 打开目录
     * @param id 记录id
     * @return 打开状态
     */
    @PostMapping("openDir")
    public Result<Boolean> openDir(Long id) {
        this.siteContentService.getById(id).ifPresent(entity -> {
            String directoryPath = String.format("%s%s\\%s", properties.getBasicPath()
                    , properties.getCrawlerPath(), entity.getContentId());
            try {
                new ProcessBuilder("explorer.exe", directoryPath).start();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });

        return success();
    }
}

